package com.hospital.ui.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.hospital.ui.adapter.MainMenuExpandableListAdapter;
import com.hospital.ui.adapter.MainMenuExpandableListAdapter.GroupHolder;

public class MainMenuExpandableList extends ExpandableListView {
	private final static String TAG = "MainMenuExpandableList";
	private final static int COLOR_GROUP_SELECTED = Color
			.parseColor("#ff337cc0");
	private final static int COLOR_GROUP_NORMAL = Color.parseColor("#ff505052");

	public int mCurrentGroupPos = -1;
	private int mCurrentChildPos = -1;

	public MainMenuExpandableList(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	public MainMenuExpandableList(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MainMenuExpandableList(Context context) {
		super(context);
	}

	public void setGroupTextSelectedColor(int groupPosition) {
		MainMenuExpandableListAdapter adapter = (MainMenuExpandableListAdapter) getExpandableListAdapter();
		View group = adapter.getGroupViewFromCache(groupPosition);
		changeGroupTextColor(group, true);
	}
	
	public void setGroupTextNormalColor(int groupPosition){
		MainMenuExpandableListAdapter adapter = (MainMenuExpandableListAdapter) getExpandableListAdapter();
		View group = adapter.getGroupViewFromCache(groupPosition);
		changeGroupTextColor(group, false);
	}

	private void changeGroupTextColor(View group, boolean isSelected) {
		if (group != null) {
			GroupHolder holder = (GroupHolder) group.getTag();
			if ((holder != null) && (holder.text != null)) {
				TextView textView = holder.text;
				if (isSelected) {
					textView.setTextColor(COLOR_GROUP_SELECTED);
				} else {
					textView.setTextColor(COLOR_GROUP_NORMAL);
				}
			}
		}
	}
	
	public void collapseCurrentGroup(){
		this.collapseGroup(mCurrentGroupPos);
	}

}
